package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import com.jiguang.shangcheng.dao.ShangChengDao;
import com.jiguang.shangcheng.pojo.Product;
import com.jiguang.shangcheng.pojo.ProductList;
import com.jiguang.shangcheng.pojo.User;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/shang/shang_index")
public class selectAllProduct extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        List<ProductList> productLists = new ArrayList<>();
        //获取搜索关键字
        String keyword = req.getParameter("keyword");

        //查询所有商品
        AdminDao adminDao = new AdminDao();
        List<Product> productList = adminDao.selectAllProduct(1, 10000);

        if(keyword != null && !keyword.equals("")){
            //查询用户
            ShangChengDao shangChengDao = new ShangChengDao();
            List<User> userList = shangChengDao.selectUserBykeyword(keyword);

            for (Product product : productList) {
                //查询商家名字
                User user = adminDao.selectUserByid(product.getUserId());

                for (User user1 : userList) {
                    if (user1.getId().equals(user.getId())){
                        ProductList products = new ProductList();
                        products.setId(product.getId());
                        products.setName(product.getName());
                        products.setPrice(product.getPrice());
                        products.setGame(product.getGame());
                        products.setPhoto(product.getPhoto());
                        products.setState(product.getState());
                        products.setCreateTime(product.getCreateTime());
                        products.setUsername(user.getNetname());
                        products.setUserId(user.getId());
                        //加入
                        productLists.add(products);
                    }
                }
            }
        }else{
            for (Product product : productList) {
                //查询商家名字
                User user = adminDao.selectUserByid(product.getUserId());
                ProductList products = new ProductList();
                products.setId(product.getId());
                products.setName(product.getName());
                products.setPrice(product.getPrice());
                products.setGame(product.getGame());
                products.setPhoto(product.getPhoto());
                products.setState(product.getState());
                products.setCreateTime(product.getCreateTime());
                products.setUsername(user.getNetname());
                products.setUserId(user.getId());
                //加入
                productLists.add(products);
            }
        }

        req.setAttribute("product",productLists);
        //返回首页
        req.getRequestDispatcher("shang_index.jsp").forward(req,resp);
    }
}
